﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfRcp.Core.Services.Commands;
using System.Windows;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Input;
using System.Threading;

namespace RibbonControls.Commands
{
    public class RibbonBasedMenuItemFactory : IMenuItemFactory
    {
        public DependencyObject GetVisualItem(IMenuItem item)
        {
            var menuItem = new RibbonButton { Label = item.Text };
            if (item.Icon != null)
            {
                menuItem.SmallImageSource = (item.Icon as System.Windows.Controls.Image).Source;
            }

            var commandDescriptor = item.Command;

            if (commandDescriptor != null)
            {
                menuItem.Command = commandDescriptor.Command;

                if (commandDescriptor.KeyGestures.Count > 0)
                {
                    var gesture = new KeyGesture(commandDescriptor.KeyGestures[0].Key, commandDescriptor.KeyGestures[0].ModifierKey);
                    Application.Current.MainWindow.InputBindings.Add(new InputBinding(menuItem.Command, gesture));
                    menuItem.KeyTip = gesture.GetDisplayStringForCulture(Thread.CurrentThread.CurrentCulture);
                }
            }

            return menuItem;
        }

        public DependencyObject GetVisualTab(IMenuTab menuItem)
        {
            var tab = new RibbonTab() { Header = menuItem.Text };
            return tab;
        }
    }

}
